package com.demo.writer_;

import java.io.*;

/**
 * @author gy
 * @date 2022/4/5
 */
public class BufferedCopy01 {
    public static void main(String[] args) {

        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;

//        String srcFileName = "f:\\20220403\\fileReader.txt";
//        String targetFileName = "f:\\20220403\\bufferedCopy01.txt";
        String srcFileName = "f:\\20220403\\谭艳 - 光明 (DJ奶小深版).mp3";
        String targetFileName = "f:\\20220403\\谭艳 - 光明 (DJ奶小深版)2.mp3";

        // 千万要注意 bufferedReader/bufferedWriter 不能copy 二进制文件，会有损失
        // 二进制文件包括：声音、视频、doc文档、pdf等等
        try {
            bufferedReader = new BufferedReader(new FileReader(srcFileName));
            bufferedWriter = new BufferedWriter(new FileWriter(targetFileName));
            String lineContext;
            while ((lineContext = bufferedReader.readLine()) != null) {
                // 此处不包含换行操作
                bufferedWriter.write(lineContext);
                // 单独添加换行操作
                bufferedWriter.newLine();
            }
            System.out.println("文件拷贝完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 最后一定要关闭流，底层调用的是  FileReader.close()
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
